package com.iwhalecloud.bss.kite.cucc.service.attr;

import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalDcPublicConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.SpringUtil;

import java.math.BigDecimal;
import java.util.Objects;

public class LocalAttrCalcFeeUtil {

    /**
     * 根据Sim卡类型计算卡号数据服务费
     * eSIM电子卡：号卡数据服务费为默认为0，
     * eSIM卡价格计算公式：号卡数据服务费=卡费（卡商+卡品联动后系统出的值）+电子卡数量*电子卡数据服务费
     */
    public static double calcESimCardFee(KiteInst inst) {

        //SIM卡类型
        KiteAttr simCardTypeAttr = inst.getAttr("200003678");
        if (Objects.isNull(simCardTypeAttr) || "100007001".equals(simCardTypeAttr.getValue())) {
            return Double.valueOf("0.00");
        }
        else if ("100007000".equals(simCardTypeAttr.getValue())) {
            String cardFee = calcCardFeeBySpec(inst);
            String elecCardFee = calcElecCardFee(inst);

            BigDecimal cardFeeDecimal = new BigDecimal(cardFee);
            BigDecimal numberDecimal = new BigDecimal(elecCardFee);

            return cardFeeDecimal.add(numberDecimal).doubleValue();
        }
        return Double.valueOf("0.00");
    }

    public static String calcCardFeeBySpec(KiteInst inst) {
        String cardFee = "0.00";
        //卡商
        KiteAttr cardMerChantAttr = inst.getAttr("200004040");
        if (Objects.isNull(cardMerChantAttr) || KiteStringUtils.isEmpty(cardMerChantAttr.getValue())) {
            return "0.00";
        }
        //实体卡规格
        KiteAttr cardEntitySpecAttr = inst.getAttr("200004041");
        if (Objects.isNull(cardEntitySpecAttr) || KiteStringUtils.isEmpty(cardEntitySpecAttr.getValue())) {
            return "0.00";
        }
        DcPublic cfg = DcPublicCache.getByPkey(LocalDcPublicConsts.SIM_CARD_FEE_CFG_STYPE, cardMerChantAttr.getValue()).stream()
            .filter(dcPublic -> cardEntitySpecAttr.getValue().equals(dcPublic.getPcode())).findFirst().orElse(null);
        if (Objects.nonNull(cfg)) {
            cardFee = cfg.getCodea();
        }
        return cardFee;
    }

    public static String calcElecCardFee(KiteInst inst) {
        //电子卡数量
        KiteAttr elecCardNumAttr = inst.getAttr("200004043");
        if (Objects.isNull(elecCardNumAttr) || KiteStringUtils.isEmpty(elecCardNumAttr.getValue())) {
            return "0.00";
        }
        String singleFee = DcPublicCache.getCodea(LocalDcPublicConsts.SIM_CARD_FEE_CFG_STYPE, "elecCardFeeSpec");
        if (KiteStringUtils.isEmpty(singleFee) || "0".equals(singleFee)) {
            return "0.00";
        }
        BigDecimal single = new BigDecimal(singleFee);
        BigDecimal number = new BigDecimal(elecCardNumAttr.getValue());
        double elecCardFeeDouble = single.multiply(number).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        return String.valueOf(elecCardFeeDouble);
    }

    public static String calcNormalSimCardFee(KiteInst inst) {
        //SIM卡类型
        KiteAttr simCardTypeAttr = inst.getAttr("200003678");
        if (Objects.isNull(simCardTypeAttr)) {
            return "0.0";
        }
        return DcPublicCache.getCodea(LocalDcPublicConsts.SIM_CARD_FEE_CFG_STYPE, simCardTypeAttr.getValue());

    }

    /**
     * 根据型号和产品服务期限，联动销售价和结算价
     * @param model 型号
     * @param serviceTime 产品服务时间
     * @param inst 产品实例
     * @param sceneInstId 场景实例id
     * @param isMain 是否是主产品，如果是修改结算价和销售价，如果不是修改公允价
     */
    public static void calculatePrice(String model, String serviceTime,  KiteInst inst, String sceneInstId, Boolean isMain) {

        OperateInstAttrService operateInstAttrService = SpringUtil.getBean(OperateInstAttrService.class);

        String salePrice = inst.getAttrValue("200004107");
        String settlePrice = inst.getAttrValue("200004108");
        /**
         * 销售价：1200元（备网001-12个月），1440元（备网001-24个月），1400元（备网002-12个月），1680元（备网002-24个月），800元（备网003-12个月），840元（备网003-24个月）
         * 结算价：990元（备网001-12个月），1080元（备网001-24个月），1200元（备网002-12个月），1440元（备网002-24个月），600元（备网003-12个月），720元（备网003-24个月），960元
         */
        switch (KiteStringUtils.join(model, SymbolConsts.HYPHEN, serviceTime)) {
            case "110007192-100000161" :
                salePrice = "1200";
                settlePrice = "990";
                break;
            case "110007192-100000162" :
                salePrice = "1440";
                settlePrice = "1080";
                break;
            case "110007193-100000161" :
                salePrice = "1400";
                settlePrice = "1200";
                break;
            case "110007193-100000162" :
                salePrice = "1680";
                settlePrice = "1440";
                break;
            case "110007194-100000161" :
                salePrice = "800";
                settlePrice = "600";
                break;
            case "110007194-100000162" :
                salePrice = "840";
                settlePrice = "720";
                break;
            default:
                break;
        }
        if (isMain) {
            operateInstAttrService.setAttrValue(inst, "200004107", salePrice,
                sceneInstId, null);
            operateInstAttrService.setAttrValue(inst, "200004108", settlePrice,
                sceneInstId, null);
        }
        else {
            // 公允价的计算规则和结算价一致
            operateInstAttrService.setAttrValue(inst, "200004115", settlePrice,
                sceneInstId, null);
        }
    }
}
